package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.CommentDao;
import com.music.pojo.Banner;
import com.music.pojo.Comment;
import com.music.service.CommentService;

import java.sql.SQLException;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    public int insert(Comment comment) {
        try {
            Comment byPrimaryKey = commentDao.getByPrimaryKey(comment.getId());
            if (byPrimaryKey == null){
                return commentDao.insert(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            Comment byPrimaryKey = commentDao.getByPrimaryKey(id);
            if (byPrimaryKey!=null){
                return commentDao.delete(id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Comment comment) {
        try {
            Comment byPrimaryKey = commentDao.getByPrimaryKey(comment.getId());
            if (byPrimaryKey!=null){
                return commentDao.update(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Comment getByPrimaryKey(Integer id) {
        try {
            Comment byPrimaryKey = commentDao.getByPrimaryKey(id);
            if (byPrimaryKey!=null){
                return commentDao.getByPrimaryKey(id);
            }
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> listPage(Comment comment, int page, int size) {
        try {
            return commentDao.listPage(comment, page, size);
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Comment> listPageInfo(Comment comment, int page, int size) {
        try {
            return commentDao.listPageInfo(comment, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> selectByExample(Comment comment) {
        Example example = createExmaple(comment);
        try {
            return commentDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Comment comment) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(comment != null){
            if(!StringUtils.isEmpty(comment.getId())){
                criteria.andEqualTo("id", comment.getId());
            }
            if(!StringUtils.isEmpty(comment.getParentId())){
                criteria.andEqualTo("parentId", comment.getParentId());
            }
            if(!StringUtils.isEmpty(comment.getUserId())){
                criteria.andEqualTo("userId", comment.getUserId());
            }
            if(!StringUtils.isEmpty(comment.getType())){
                criteria.andEqualTo("type", comment.getType());
            }
            if(!StringUtils.isEmpty(comment.getContent())){
                criteria.andEqualTo("content", comment.getContent());
            }
            if(!StringUtils.isEmpty(comment.getLikeCount())){
                criteria.andEqualTo("likeCount", comment.getLikeCount());
            }
            if(!StringUtils.isEmpty(comment.getTime())){
                criteria.andEqualTo("time", comment.getTime());
            }
            if(!StringUtils.isEmpty(comment.getStatus())){
                criteria.andEqualTo("status", comment.getStatus());
            }
        }
        return example;
    }

    /**
     * Comment 构建查询对象
     * @param comment
     * @return
     */
    public Example createExample(Comment comment){
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(comment != null){
            if(!StringUtils.isEmpty(comment.getId())){
                criteria.andEqualTo("id", comment.getId());
            }
            if(!StringUtils.isEmpty(comment.getParentId())){
                criteria.andEqualTo("parentId", comment.getParentId());
            }
            if(!StringUtils.isEmpty(comment.getUserId())){
                criteria.andEqualTo("userId", comment.getUserId());
            }
            if(!StringUtils.isEmpty(comment.getType())){
                criteria.andEqualTo("type", comment.getType());
            }
            if(!StringUtils.isEmpty(comment.getContent())){
                criteria.andEqualTo("content", comment.getContent());
            }
            if(!StringUtils.isEmpty(comment.getLikeCount())){
                criteria.andEqualTo("likeCount", comment.getLikeCount());
            }
            if(!StringUtils.isEmpty(comment.getTime())){
                criteria.andEqualTo("time", comment.getTime());
            }
            if(!StringUtils.isEmpty(comment.getStatus())){
                criteria.andEqualTo("status", comment.getStatus());
            }
        }
        return example;
    }
}
